'use strict';
const knowError = {
  200: 'OK',
  400: 'bad_request',
  401: 'unauthorized',
  403: 'forbidden',
  404: 'not_found',
  500: 'internel_server_error',
  503: 'service_unavailable',
};

module.exports = function ApiErrorHandler(err, req, res, next) {
  knowError[err.code] || (err.code = 500);
  err.error || (err.error = knowError[err.code]);
  err.error_description || (err.error_description = err.message || 'unknow error');
  console.error(err);
  // res.status(err.code).send(err);
  res.status(err.code).send({ code: err.code, error: err.error, error_description: err.error_description });
  next && next();
};
